<template>
  <div class="conversation-detail" v-loading="loading">
    <div v-if="detail" class="detail-container">
      <div class="header">
        <h3>对话详情 (ID: {{ detail.id }})</h3>
        <div class="meta">
          <span>用户: {{ detail.userName }} (ID: {{ detail.userId }})</span>
          <span>消息数: {{ detail.messageCount }}</span>
          <span>时间: {{ formatTime(detail.startTime) }} 至 {{ formatTime(detail.updateTime) }}</span>
        </div>
      </div>
      
      <div class="messages" ref="messagesContainer" :class="{ scrollable: isScrollable }">
        <div v-for="(msg, index) in messages" :key="index" class="message" :class="msg.role">
          <div class="avatar">
            <el-icon v-if="msg.role === 'user'">
              <User />
            </el-icon>
            <el-icon v-else>
              <ChatLineRound />
            </el-icon>
          </div>
          <div class="content">
            <div class="text">{{ msg.content }}</div>
            <div class="time">{{ formatTime(msg.time) }}</div>
          </div>
        </div>
      </div>
    </div>

    <div v-else class="empty-state">
      <el-icon class="empty-icon">
        <ChatLineRound />
      </el-icon>
      <p class="empty-text">请选择对话</p>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, nextTick } from 'vue'
import { formatTime } from '@/utils/date'
import { User, ChatLineRound } from '@element-plus/icons-vue'

const props = defineProps({
 detail: Object,
  messages: Array,
  loading: Boolean
})

    const messagesContainer = ref(null)
    const isScrollable = ref(false)

    function checkScrollable() {
      const el = messagesContainer.value
      if (el) isScrollable.value = el.scrollHeight > el.clientHeight + 2
    }

    function scrollToBottom() {
      const el = messagesContainer.value
      if (el) el.scrollTop = el.scrollHeight
    }

    watch(() => props.messages, () => {
      nextTick(() => {
        checkScrollable()
        scrollToBottom()
      })
    }, { deep: true })

    nextTick(() => {
      checkScrollable()
      scrollToBottom()
    })
    </script>

<style scoped>
.conversation-detail {
  height: 100%;
  padding: 0;
}

.detail-container {
  padding: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.header {
  margin-bottom: 0;
  padding: 20px;
  border-bottom: 1px solid rgba(0, 212, 255, 0.3);
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.9) 0%, rgba(0, 40, 80, 0.9) 100%);
  border-radius: 12px 12px 0 0;
}

.header h3 {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
  color: #ffffff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.meta {
  display: flex;
  gap: 16px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  flex-wrap: wrap;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.messages {
  flex: 1;
  padding: 20px;
  padding-bottom: 64px; /* 防止被底部输入框遮挡，按实际输入框高度调整 */
  overflow-y: auto;
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.8) 0%, rgba(0, 40, 80, 0.8) 100%);
  border-radius: 0 0 12px 12px;
}

.message {
  display: flex;
  margin-bottom: 20px;
  animation: fadeIn 0.3s ease;
  animation-fill-mode: both;
}

.message:nth-child(odd) {
  animation-delay: 0.1s;
}

.message:nth-child(even) {
  animation-delay: 0.2s;
}

.message.user {
  justify-content: flex-end;
}

.message.user .content {
  background: linear-gradient(135deg, #3b82f6 0%, #6366f1 100%);
  color: #ffffff;
  border-radius: 12px 12px 0 12px;
}

.message.assistant .content {
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.9) 0%, rgba(0, 40, 80, 0.9) 100%);
  color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 212, 255, 0.2);
  border-radius: 12px 12px 12px 0;
  border: 1px solid rgba(0, 212, 255, 0.3);
}

.avatar {
  margin: 0 12px;
  display: flex;
  align-items: flex-start;
  padding-top: 4px;
}

.avatar .el-icon {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.9) 0%, rgba(0, 40, 80, 0.9) 100%);
  border-radius: 50%;
  color: rgba(0, 212, 255, 0.8);
  border: 1px solid rgba(0, 212, 255, 0.4);
}

.message.user .avatar .el-icon {
  background: linear-gradient(135deg, rgba(0, 212, 255, 0.3) 0%, rgba(0, 153, 204, 0.3) 100%);
  color: #00d4ff;
  border: 1px solid rgba(0, 212, 255, 0.6);
}

.content {
  max-width: 75%;
  padding: 12px 16px;
  position: relative;
  transition: all 0.3s ease;
}

.text {
  word-break: break-word;
  line-height: 1.6;
}

.time {
  margin-top: 6px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.6);
  text-align: right;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.message.user .time {
  color: rgba(255, 255, 255, 0.7);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 空状态样式 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: linear-gradient(135deg, rgba(0, 212, 255, 0.2) 0%, rgba(0, 153, 204, 0.2) 100%);
  border: 1px solid rgba(0, 212, 255, 0.4);
  border-radius: 12px;
  padding: 40px;
}

.empty-icon {
  font-size: 64px;
  color: rgba(0, 212, 255, 0.6);
  margin-bottom: 16px;
}

.empty-text {
  color: #ffffff;
  font-size: 16px;
  font-weight: 500;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  margin: 0;
}

@media (max-width: 768px) {
  .header {
    padding: 16px;
  }

  .messages {
    padding: 16px;
  }

  .content {
    max-width: 85%;
  }
}
.messages {
  overflow-y: hidden;
}
.messages.scrollable {
  overflow-y: auto;
}
</style>